package fei.yuzhi.com.loveflowers.fragment.mefragment.login.register;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.sms.BmobSMS;
import cn.bmob.v3.Bmob;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.base.BaseActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.register.xieyi.XieYiActivity;


public class RegisterActivity extends BaseActivity implements RegisterContract.ViewRegister {

    @BindView(R.id.iv_register_back)
    ImageView ivRegisterBack;
    @BindView(R.id.et_register_userName)
    EditText etRegisterUserName;
    @BindView(R.id.et_register_userTell)
    EditText etRegisterUserTell;
    @BindView(R.id.et_register_userDuanXin)
    EditText etRegisterUserDuanXin;
    @BindView(R.id.iv_register_userDuanXin)
    ImageView ivRegisterUserDuanXin;
    @BindView(R.id.et_register_userPass)
    EditText etRegisterUserPass;
    @BindView(R.id.et_register_userPass_again)
    EditText etRegisterUserPassAgain;
    @BindView(R.id.cb_register_userAgree)
    CheckBox cbRegisterUserAgree;
    @BindView(R.id.tv_register_userXieYi)
    TextView tvRegisterUserXieYi;
    @BindView(R.id.btn_register_userYes)
    Button btnRegisterUserYes;
    @BindView(R.id.tv_register_userWiteAgain)
    TextView tvRegisterUserWiteAgain;

    private final int XIEYI_RETURN=0;
    private RegisterPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "7c6d1a4bf3aafd9f814667d6ad3cf6bb");
        BmobSMS.initialize(this,"7c6d1a4bf3aafd9f814667d6ad3cf6bb");

        presenter = new RegisterPresenter(this);


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }
    @Override
    public void onVerticalSucess(String msg) {

        if ("注册成功".equals(msg)){
            Toast.makeText(RegisterActivity.this,msg+",请登录", Toast.LENGTH_SHORT).show();
            finish();
        }else {
            Toast.makeText(RegisterActivity.this,msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void sendSituation(String msg) {
        Toast.makeText(RegisterActivity.this,msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.iv_register_back, R.id.iv_register_userDuanXin, R.id.tv_register_userXieYi, R.id.btn_register_userYes, R.id.tv_register_userWiteAgain})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_register_back:
                finish();
                break;
            case R.id.iv_register_userDuanXin:
                sendDuanXin();
                break;
            case R.id.tv_register_userXieYi:
                Intent intent=new Intent(RegisterActivity.this, XieYiActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_register_userYes:
                zhuce();
                break;
            case R.id.tv_register_userWiteAgain:
                 etRegisterUserName.setText("");
                 etRegisterUserTell.setText("");
                 etRegisterUserDuanXin.setText("");
                 etRegisterUserPass.setText("");
                 etRegisterUserPassAgain.setText("");
                break;
        }
    }

    private void sendDuanXin() {
        String path = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        String tell=etRegisterUserTell.getText().toString().trim();
        Pattern pattern = null;
        boolean flag =  pattern.compile(path).matcher(tell).matches();
        //手机号判断
        if(flag) {
            presenter.sendYanZhengMa(RegisterActivity.this,tell);
        }else {
        Toast.makeText(RegisterActivity.this, "手机号不正确", Toast.LENGTH_SHORT).show();
    }
    }

    private void zhuce() {
        String path = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        Pattern pattern = null;
        boolean flag =  pattern.compile(path).matcher(etRegisterUserTell.getText().toString().trim()).matches();
        String name = etRegisterUserName.getText().toString().trim();
        String pass = etRegisterUserPass.getText().toString();
        String tell=etRegisterUserTell.getText().toString().trim();

        //判空
        if (isNull(etRegisterUserName)&&isNull(etRegisterUserPass)) {
            //手机号验证
            if (flag) {
            //验证两次密码一致
                if (etRegisterUserPass.getText().toString().equals(etRegisterUserPassAgain.getText().toString())) {
                    //阅读协议
                    if (cbRegisterUserAgree.isChecked()) {
                        //验证验证码
//                        BmobSMS.verifySmsCode(RegisterActivity.this,etRegisterUserTell.getText().toString().trim(),etRegisterUserDuanXin.getText().toString().trim(), new VerifySMSCodeListener() {
//                            @Override
//                            public void done(cn.bmob.sms.exception.BmobException e) {
//                                // TODO Auto-generated method stub
//                                if(e==null){//短信验证码已验证成功
//                                    Log.i("bmob", "验证通过");

                       presenter.userRegister(RegisterActivity.this,name,pass,tell);


//                                }else{
//                                    Toast.makeText(RegisterActivity.this, "短信验证码验证错误", Toast.LENGTH_SHORT).show();
//                                    Log.i("bmob", "验证失败：code ="+e.getErrorCode()+",msg = "+e.getLocalizedMessage());
//                                }
//                            }
//                        });

                    }else {
                        Toast.makeText(RegisterActivity.this, "请确认阅读协议", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(RegisterActivity.this, "两次输入密码不正确", Toast.LENGTH_SHORT).show();
                }
        }else {
                Toast.makeText(RegisterActivity.this, "手机号不正确", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(RegisterActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 判断文本框的内容是否为空
     *
     * @param editText
     *            需要判断是否为空的EditText对象
     * @return boolean 返回是否为空,空(true),非空(false)
     */
    public static boolean isNull(EditText editText) {
        String text = editText.getText().toString().trim();
        if (text != null && text.length() > 0) {
            return true;
        }
        return false;
    }





}
